package com.xsd.rabbitmq.routingkey;

import com.rabbitmq.client.*;
import com.xsd.rabbitmq.util.ConnectionUtil;

import java.io.IOException;

/**
 * @Description: 消费者
 * @Author: zhoufuzong
 * @Created: 2021-03-14 14:16:16
 * @Version: 1.1.0
 */

public class Counsumer2 {

    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        // 声明交换机
        channel.exchangeDeclare(Producer.DIRECT_EXCHANGE, BuiltinExchangeType.DIRECT);
        // 声明队列
        channel.queueDeclare(Producer.DIRECT_QUEUE_UPDATE,true,false,false,null);
        // 队列绑定交换机
        channel.queueBind(Producer.DIRECT_QUEUE_UPDATE,Producer.DIRECT_EXCHANGE,"update");
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("路由key="+envelope.getRoutingKey());
                System.out.println("交换机="+envelope.getExchange());
                System.out.println("消息id="+envelope.getDeliveryTag());
                System.out.println("消费者2 ----收到消息="+new String(body,"utf-8"));

            }
        };
        // 监听队列
        channel.basicConsume(Producer.DIRECT_QUEUE_UPDATE,true,consumer);



    }
}
